package com.zpf.dao.impl;

import com.zpf.dao.AdminDao;
import com.zpf.pojo.Address;
import com.zpf.pojo.Goods;
import com.zpf.pojo.GoodsType;
import com.zpf.pojo.User;
import com.zpf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {
    QueryRunner qf = new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public User adminselectByUsernameAndPassword(String username, String password) {

        String sql = "select id, username,password,email,gender,flag, role,code from tb_user where username = ? and password = md5(?) and role=0";
        try {
            User user= (User) qf.query(sql, new BeanHandler<>(User.class), username,password);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("检查用户名是否存在失败!!!");
        }

        return null;
    }

    @Override
    public List<User> findAdminByRole() {

        String sql = "select id, username,password,email,gender,flag, role,code from tb_user where role=1 and flag = 1";
        try {
           List<User> user= qf.query(sql, new BeanListHandler<>(User.class));
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询会员列表失败!!!");
        }
        return null;
    }

    @Override
    public List<GoodsType> AdminByShow() {

        String sql = "\n" +
                "select \n" +
                "  `id`,\n" +
                "  `name`,\n" +
                "  `level`,\n" +
                "  `Parent` \n" +
                "from\n" +
                "  `db_shopping`.`tb_goods_type`  ";
        try {
            return qf.query(sql, new BeanListHandler<>(GoodsType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品列表失败!!!");
        }
        return null;
    }

    @Override
    public List<Goods> getAdminGoodsList() {

        String sql = "SELECT \n" +
                "  `id`,\n" +
                "  `name`,\n" +
                "  `pubdate`,\n" +
                "  `picture`,\n" +
                "  `price`,\n" +
                "  `star`,\n" +
                "  `intro`,\n" +
                "  `typeid` \n" +
                "FROM\n" +
                "  `db_shopping`.`tb_goods` \n" ;
        try{
            List<Goods> goods= qf.query(sql, new BeanListHandler<>(Goods.class));
            return goods;
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品列表失败!!!");
        }
        return null;
    }

    @Override
    public List<GoodsType> AdminAdd() {
        String sql = "\n" +
                "select \n" +
                "  `id`,\n" +
                "  `name`,\n" +
                "  `level`,\n" +
                "  `Parent` \n" +
                "from\n" +
                "  `db_shopping`.`tb_goods_type` ";
        try {
            return qf.query(sql, new BeanListHandler<>(GoodsType.class));
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询商品列表失败!!!");
        }
        return null;
    }

    @Override
    public void insert(Address address) {
        String sql = "INSERT INTO `db_shopping`.`tb_goods_type` (`id`, `name`, `level`, `Parent`) VALUES (null , ?, ?, null);\n";
        try {
            qf.update(sql,address.getName(),address.getLevel());
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增失败");
        }


    }
}
